package common.interfaces;

/**
 * An abstract implementation that also overrides hashCode and equals based on the key
 * 
 * @author Gal Lalouche
 * 
 * @param <T>
 */
public abstract class AbstractHasID<T> implements IHasId<T> {

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		@SuppressWarnings("unchecked")
		AbstractHasID<T> other = (AbstractHasID<T>)obj;
		if (getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!getId().equals(other.getId()))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return getClass().getCanonicalName() + "[" + getId() + "]";
	}
}
